﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using SqlSugar.IOC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wq.Core.Common;

namespace Wq.Core.Extensions.ServiceExtensions
{
    /// <summary>
    /// SqlSugar注册
    /// </summary>
    public static class SqlSugarSetup
    {
        public static void AddSqlSugar(this IServiceCollection services)
        {
            services.AddSqlSugar(new IocConfig()
            {
                ConfigId = "platadmin",
                ConnectionString = Appsettings.app("ConnectionStrings:ManagerEntities"),
                DbType = IocDbType.MySql,
                IsAutoCloseConnection = true
            });
            services.AddSqlSugar(new IocConfig()
            {
                ConfigId = "user",
                ConnectionString = Appsettings.app("ConnectionStrings:UserEntities"),
                DbType = IocDbType.MySql,
                IsAutoCloseConnection = true
            });

            //设置参数
            services.ConfigurationSugar(db =>
            {
                db.GetConnection("platadmin").Aop.OnLogExecuting = (sql, p) =>
                {
                    Console.WriteLine(sql);
                };
                db.GetConnection("user").Aop.OnLogExecuting = (sql, p) =>
                {
                    Console.WriteLine(sql);
                };

            });
        }
    }
}
